<?php
namespace app\index\controller;

use think\Controller;
use think\Request;

class Pay extends Base
{
    public function index(Request $request)
    {
        $user = $this->getLoginUser();
        if (!$user)
        {
            $this->error('请登录', 'user/login');
        }

        $orderId = $request->param('id');
        if (empty($orderId))
        {
            $this->error('请求不合法');
        }
        $order = model('Order')->get($orderId);
        if (empty($order) || $order['status'] != 1 || $order['pay_status'] != 0)
        {
            $this->error('无法进行该操作');
        }
        // 判定 订单是否是用户本人
        if ($order['username'] != $user['username'])
        {
            $this->error('非本人订单');
        }
        $deal = model('Deal')->get($order['deal_id']);

        // TODO:生成二维码
        /*
                 $input = new \WxPayUnifiedOrder();
        $input->SetBody($deal['name']);
        $input->SetAttach($deal['name']);
        $input->SetOut_trade_no($order['out_trade_no']); //设置订单编号
        $input->SetTotal_fee($order['total_price'] * 100);//支付金额
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("test");
        $notify_url = $http."http://localhost/index.php/index/weixinpay/notify";
        $input->SetNotify_url($notify_url );//设置回调
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id($order['deal_id']);//设置产品ID编号
        $result = $notify->GetPayUrl($input);
        if (empty($result["code_url"])) {
            $url = '';
        } else {
            $url = $result["code_url"];
        }
         */
        $this->assign([
            'deal' => $deal,
            'order' => $order
            // 'url' => $url,
        ]);
        // 调取二维码
        return $this->fetch();
    }

    public function paysuccess()
    {
        if (!$this->getLoginUser())
        {
            $this->error('请登录', 'user/login');
        }
        return $this->fetch();
    }

}
